<?php
class Core_Model_Object {

	private $_data = array();

	public function getData($key=null) {
		if ($key)
			return isset($this->_data[$key]) ? $this->_data[$key] : null;
		else
			return $this->_data;
	}

	public function setData($key, $value) {
		$this->_data[$key] = $value;
		return $this;
	}

	public function addData(array $data) {
		$this->_data = array_merge($this->_data, $data);
		return $this;
	}

	public function hasData($key) {
		return array_key_exists($key, $this->_data);
	}
	
	public function unsetData($key) {
		unset($this->_data[$key]);
		return $this;
	}
	
	public function __call($method, $args) {
		if (method_exists($this, $method))
			return $this->$method($args);
		
		$key = $this->_underscore(substr($method,3));
		
		switch (substr($method, 0, 3)) {
			case 'get' :
				$data = $this->getData($key, isset($args[0]) ? $args[0] : null);
				return $data;

			case 'set' :
				$result = $this->setData($key, isset($args[0]) ? $args[0] : null);
				return $result;

			case 'has' :
				return $this->hasData($key);

			case 'unset' :
				return $this->unsetData($key);
		}
		throw new Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
	}

	protected function _underscore($name) {
		$result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
		return $result;
	}

}